//
//  HAPopBaseView.m
//  HA
//
//  Created by sy on 2023/12/17.
//

#import "HAPopBaseView.h"

@implementation HAPopBaseView

-(instancetype)init {
    if ([super init]) {
    }
    return self;
}

-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

-(void)setupUI {
    self.frame = CGRectMake(0, 0, kUIScreen_w, kUIScreen_h);
    [self addSubview:self.backgroundView];
}

#pragma mark - 点击背景遮罩图层事件
- (void)didTapBackgroundView:(UITapGestureRecognizer *)sender {
}


#pragma mark - 背景遮罩图层
- (UIView *)backgroundView {
    if (!_backgroundView) {
        _backgroundView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, kUIScreen_w, kUIScreen_h)];
        _backgroundView.backgroundColor = [UIColor colorWithWhite:0 alpha:0.70];
        _backgroundView.userInteractionEnabled = YES;
        UITapGestureRecognizer *myTap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(didTapBackgroundView:)];
        [_backgroundView addGestureRecognizer:myTap];
    }
    return _backgroundView;
}

#pragma mark - 弹出视图
- (UIView *)alertView {
    if (!_alertView) {
        _alertView = [UIView ha_createView];
        _alertView.backgroundColor = [UIColor whiteColor];
        _alertView.layer.cornerRadius = 5;
        _alertView.layer.masksToBounds = YES;
    }
    return _alertView;
}

-(void)show {

    UIWindow *keyWindow =  [[[UIApplication sharedApplication] windows] objectAtIndex:0];

    [keyWindow addSubview:self];
    CGRect rect = self.alertView.frame;
    rect.origin.y = kUIScreen_h;
    self.alertView.frame = rect;
    [UIView animateWithDuration:0.3 animations:^{
        CGRect rect = self.alertView.frame;
        rect.origin.y = self.alertViewY;
        self.alertView.frame = rect;
    }];
}
-(void)dismiss {
    [UIView animateWithDuration:0.25 animations:^{
        CGRect rect = self.alertView.frame;
        rect.origin.y += kUIScreen_h;
        self.alertView.frame = rect;
        self.backgroundView.alpha = 0;
    } completion:^(BOOL finished) {
        for (UIView *view in self.subviews) {
            [view removeFromSuperview];
        }
        [self removeFromSuperview];
    }];
}


@end
